# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: Administrator
"""
#%%

import os
import glob
from ..origin import Shell_script

#%%
class Petpvc(Shell_script):
    #作用原理：
    #参数说明：
    #参数说明：
    #返回值说明：
    #举例：
    #调用：
    #被调用：
    #bug：
    petpvc_str = 'petpvc'
    def __init__(self,input_list):
        print('input_list:',input_list)
        if len(input_list) != 2 :
            raise Exception("the number of elements of input_list is not equal to 3")
        self.initialize_input_list(input_list)
        self.concat_inputs()
        for self.input_filepath,self.mask_filepath in self.input_filepaths,self.mask_filepaths:
          front,end = os.path.splitext(os.path.split(self.input_filepath)[1])
          self.output_filepath = self.output_dirpath + self.os_sep + front + self.underline + \
                                 self.petpvc_str + self.linkline + self.algorithm_str + self.linkline\
                                 ''.join([str(i) for i in self.fwhm_list]) + end
          self.concat_script()
          self.excute_shell_script()
        
    def initialize_input_list(self,input_list):
        
        #重新整理传入参数
        self.input_dirpath = input_list[0]
        self.mask_dirpath = input_list[1]
        self.algorithm_str = input_list[2]
        self.fwhm_list = input_list[3]
        self.output_dirpath = input_list[4]
        
    def concat_inputs(self):
      
        #下面是使用self.get_filepath这个函数获取self.input_dirpath self.mask_filepaths
        blur_dirpath = self.input_dirpath+self.os_sep+self.star+self.suffix
        self.input_filepaths = self.get_filepath(blur_dirpath)
        print('self.input_filepath:',self.input_filepath)
        blur_dirpath = self.mask_dirpath+self.os_sep+self.star+self.suffix
        self.mask_filepaths = self.get_filepath(blur_dirpath)
        print('self.mask_filepaths:',self.mask_filepaths)
        
        #下面是判断self.input_dirpath self.mask_dirpath内含有相等的文件数
        if not len(self.input_filepaths) == len(self.mask_filepaths):
          raise Exception('input_dipath maskdirpath must have same amount of file')
        
        self.output_dirpath = self.output_dirpath + self.os_sep + os.path.split(self.input_dirpath)[1] + \
                              + self.underline + self.petpvc_str + self.linkline + self.algorithm_str + \
                              self.linkline + ''.join([str(i) for i in self.fwhm_list]) 
        #如果self.output_dirpath不存在，则新建
        if not os.path.exists(self.output_dirpath):
            os.makedirs( self.output_dirpath)

    def concat_script(self):
        
        #cmd_1的内容：petpvc -i <PET> -m <MASK> -o <OUTPUT> --pvc IY -x 6.0 -y 6.0 -z 6.0 
        self.cmd_1 =  r'petpvc -i ' + self.input_filepath + ' -m ' + self.mask_filepath + ' -o' + \
                      self.output_filepath + ' -p ' +  self.algorithm_str + \
                      ' -x ' + str(self.fwhm_list[0]) + ' -y ' + str(self.fwhm_list[1]) + \
                      ' -z ' + str(self.fwhm_list[2])
        
        self.shell_script = self.cmd_1
    
    @staticmethod
    def get_filepath(self,blur_dirpath):
        print(blur_dirpath) 
        filepaths = glob.glob(blur_dirpath)
        filepaths.sort()
        print('filepaths:',filepaths)
        return filepaths
